package com.ipt.frm.pw;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class PrimerServlet
 */
public class RequestInfo extends ServletMaestro {

	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RequestInfo() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<body>");
		out.println("<head>");
		out.println("<title>Request Information Example</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h3>Datos insertados en el formulario enviados mediante GET</h3>");

		Map valores = request.getParameterMap();
		Iterator it = valores.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry e = (Map.Entry) it.next();
			String key = e.getKey().toString();
			String[] valor = (String[]) e.getValue();
			out.println("Campo: " + key + ", valor: " + valor[0].toString()
					+ "<br/>");
		}

		/*
		 * out.println("Tu nombre es: " + request.getParameter("nombre") +
		 * "<br/>"); out.println("Tu email es: " + request.getParameter("email")
		 * + "<br/>"); out.println("Tu sexo es: " + request.getParameter("sexo")
		 * + "<br/>");
		 */

		// print session info

		HttpSession session = request.getSession(true);
		Date created = new Date(session.getCreationTime());
		Date accessed = new Date(session.getLastAccessedTime());
		out.println("ID " + session.getId());
		out.println("Created: " + created);
		out.println("Last Accessed: " + accessed);

		// guardar en session Atributo recogido por formulario
		session.setAttribute("user", request.getParameter("nombre"));
		out.println("</body>");
		out.println("</html>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// Puenteado las peticiones POST a doGet
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<body>");
		out.println("<head>");
		out.println("<title>Request Information Example</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h3>Datos insertados en el formulario enviados mediante POST</h3>");
		out.println("Tu nombre es: " + request.getParameter("nombre") + "<br/>");
		out.println("Tu email es: " + request.getParameter("email") + "<br/>");
		out.println("Tu sexo es: " + request.getParameter("sexo") + "<br/>");
		out.println("</body>");
		out.println("</html>");
	}

}
